package com.laker.admin.module.excel.facade.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.laker.admin.framework.model.Response;
import com.laker.admin.module.excel.application.ExcelFillApplication;
import com.laker.admin.module.excel.domain.ExcelFill;
import com.laker.admin.module.excel.entity.ExcelFillCmd;
import com.laker.admin.module.excel.facade.ExcelFillFacade;
import com.laker.admin.module.ext.entity.ExtLeave;
import com.laker.admin.module.ext.service.IExtLeaveService;
import com.laker.admin.module.flow.process.SnakerEngineFacets;
import com.laker.admin.module.sys.service.ISysUserService;
import org.snaker.engine.entity.HistoryOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author nihongxin
 * @date 2021/12/14
 * @description
 */
@Service
public class ExcelFillFacadeImpl implements ExcelFillFacade {

    @Resource
    private ExcelFillApplication excelFillApplication;

    @Autowired
    private IExtLeaveService extLeaveService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SnakerEngineFacets snakerEngineFacets;


    @Override
    public Response save(ExcelFill excelFill) {
        try {
            excelFill.setCreateBy(StpUtil.getLoginIdAsLong());
            return Response.ok(excelFillApplication.save(excelFill));
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response deleteById(Long id) {
        try {
            excelFillApplication.deleteById(id);
            return Response.ok("删除成功");
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response getById(Long id) {
        try {
            return Response.ok(excelFillApplication.getById(id));
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response list(ExcelFillCmd excelFillCmd) {
        try {
            Page<ExcelFill> excelFillPage = excelFillApplication.listExcelFillPage(excelFillCmd.getKeyword(), excelFillCmd.getPageNo(), excelFillCmd.getPageSize());
            Set<Long> excelFillIdSet = excelFillPage.getContent().stream().map(ExcelFill::getId).collect(Collectors.toSet());
            if (!excelFillIdSet.isEmpty()) {
                List<ExtLeave> extLeaveList = extLeaveService.list(Wrappers.<ExtLeave>lambdaQuery().in(ExtLeave::getExcelFillId, excelFillIdSet));
                extLeaveList.forEach(extLeave -> {
                    extLeave.setCreateUser(sysUserService.getUserAndDeptById(extLeave.getCreateBy()));
                    // 流程实例状态
                    HistoryOrder histOrder = snakerEngineFacets.getEngine().query().getHistOrder(extLeave.getOrderId());
                    excelFillPage.getContent().stream().filter(o -> o.getId().equals(extLeave.getExcelFillId()))
                            .forEach(x -> {
                                x.setOrderState(histOrder.getOrderState());
                                x.setOrderId(histOrder.getId());
                            });
                });
            }
            return Response.ok(excelFillPage);
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }

    @Override
    public Response getByOrderId(String orderId) {
        try {
            ExtLeave extLeave = extLeaveService.getOne(Wrappers.<ExtLeave>lambdaQuery().eq(ExtLeave::getOrderId, orderId));
            ExcelFill excelFill = excelFillApplication.getById(extLeave.getExcelFillId());
            return Response.ok(excelFill);
        } catch (Exception e) {
            return Response.error(e.getMessage());
        }
    }
}
